'''
Created on May 4, 2011

@author: peterd
'''

from django.contrib import admin
from django import forms

from mhd.models import MHD_Stop_Type, MHD_Link, MHD_Stop, MHD_Link_Stop, MHD_Link_Direction
from mhd.adminforms import MHD_LinkForm

class MHD_StopTypeAdmin(admin.ModelAdmin):
    list_display = ('name',)
    list_filter = ('name',)
    search_fields = ('name',)
    ordering = ['name']
    

class MHD_LinkAdmin(admin.ModelAdmin):
    list_display = ('name',)
    list_filter = ('name',)
    search_fields = ('name',)
    ordering = ['name']
    
    form = MHD_LinkForm
    

    def change_view(self, request, object_id, extra_context=None):
        #bytovyDom = BytovyDom.objects.get(id = object_id)
        mhd_link_direction = MHD_Link_Direction.objects.filter(link__name = object_id)[0]
        mhd_link_stops = MHD_Link_Stop.objects.filter(link__name = object_id, direction = mhd_link_direction).order_by('stop_number')
        stops = []
        for mhd_link_stop in mhd_link_stops:
            stops.append(mhd_link_stop.as_json())
        return super(MHD_LinkAdmin, self).change_view(request, object_id, extra_context = {
                                                                'mhd_link_stops': mhd_link_stops, 
                                                                'link_name': object_id,
                                                                'direction_id': mhd_link_direction.id,
                                                                'stops': stops})
    

class MHD_StopAdmin(admin.ModelAdmin):
    list_display = ('name', 'lat', 'lng', 'stop_type', 'hint')
    list_filter = ('lat',)
    search_fields = ('name',)
    ordering = ['name']
    

class MHD_LinkStopAdmin(admin.ModelAdmin):
    list_display = ('link', 'stop', 'stop_number')
    list_filter = ('link','stop',)
    search_fields = ('link', 'stop',)
    ordering = ['link']


admin.site.register(MHD_Stop_Type, MHD_StopTypeAdmin)
admin.site.register(MHD_Link, MHD_LinkAdmin)
admin.site.register(MHD_Stop, MHD_StopAdmin)
admin.site.register(MHD_Link_Stop, MHD_LinkStopAdmin)